package cn.lf.android.lawfirm.worklog.controller;


import android.content.Context;

import com.alibaba.fastjson.JSON;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import cn.lf.android.lawfirm.AppConfig;
import cn.lf.android.lawfirm.BaseController;
import cn.lf.android.lawfirm.http.manager.EBCallback;
import cn.lf.android.lawfirm.http.manager.MyOkHttpClient;
import cn.lf.android.lawfirm.undertake.approval.modle.LawyerFeeInfo;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.utils.SPUtil;
import cn.lf.android.lawfirm.worklog.molde.WorkLogDetail;

/**
 * 添加
 */
public class WorkLogInfoConroller extends BaseController {
    WorkLogInfoCallback mWorkLogInfoCallback;
    WorkLogApproveCallback mWorkLogApproveCallback;
    private Context context;

    public WorkLogInfoConroller(WorkLogInfoCallback callback, WorkLogApproveCallback callback1, Context context) {
        this.mWorkLogInfoCallback = callback;
        this.mWorkLogApproveCallback = callback1;
        this.context = context;
    }


    /**
     * @param logID
     */
    public void onWorkLogDetail(String logID) {
        showProgressDia(context);
        Map<String, String> params = new HashMap<String, String>();
        SPUtil spUtil = new SPUtil(context);
        params.put("logID", logID);
        new MyOkHttpClient().doGet(AppConfig.LsysLog_Detail, params,
                new EBCallback() {
                    @Override
                    public void onEBResponse(String response) {
                        DebugLog.e("工作日誌onWorkLogDetail===" + response);
                        JSONObject myJsonObject;
                        JSONObject responsObject;
                        Boolean isSuc;
                        try {
                            myJsonObject = new JSONObject(response);
                            isSuc = myJsonObject.getBoolean("isSuc");
                            if (isSuc) {
                                responsObject = myJsonObject.getJSONObject("responsObject");
                                WorkLogDetail bean = JSON.parseObject(responsObject.toString(), WorkLogDetail.class);
                                mWorkLogInfoCallback.onorkLogInfoSuccessed(bean);
                            } else {
                                mWorkLogInfoCallback
                                        .onWorkLogInfoFailed();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        dismissProgressDia();
                    }

                    @Override
                    public void onEBError(String errorMsg) {
                        mWorkLogInfoCallback.onWorkLogInfoFailed();
                        dismissProgressDia();
                    }
                }, "Authorization", spUtil.getAccessToken());
    }

    /**
     * @param logID
     */
    public void onWorkLogDetailInfo(String logID) {
        showProgressDia(context);
        Map<String, String> params = new HashMap<String, String>();
        SPUtil spUtil = new SPUtil(context);
        params.put("logID", logID);
        new MyOkHttpClient().doGet(AppConfig.LsysLog_DetailInfo, params,
                new EBCallback() {
                    @Override
                    public void onEBResponse(String response) {
                        DebugLog.e("工作日誌LsysLog_DetailInfo===" + response);
                        JSONObject myJsonObject;
                        JSONObject responsObject;
                        Boolean isSuc;
                        try {
                            myJsonObject = new JSONObject(response);
                            isSuc = myJsonObject.getBoolean("isSuc");
                            if (isSuc) {
                                responsObject = myJsonObject.getJSONObject("responsObject");
                                WorkLogDetail bean = JSON.parseObject(responsObject.toString(), WorkLogDetail.class);
                                mWorkLogInfoCallback.onorkLogInfoSuccessed(bean);
                            } else {
                                mWorkLogInfoCallback
                                        .onWorkLogInfoFailed();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        dismissProgressDia();
                    }

                    @Override
                    public void onEBError(String errorMsg) {
                        mWorkLogInfoCallback.onWorkLogInfoFailed();
                        dismissProgressDia();
                    }
                }, "Authorization", spUtil.getAccessToken());
    }

    /**
     * 审批操作
     *
     * @param id
     * @param LogType
     * @param strchecknum
     */
    public void onWorkLogApprove(String id, String LogType, String strchecknum) {
        showProgressDia(context);
        Map<String, String> params = new HashMap<String, String>();
        SPUtil spUtil = new SPUtil(context);
        params.put("id", id);
        params.put("LogType", LogType);
        params.put("strchecknum", strchecknum);
        DebugLog.e("id==" + id);
        DebugLog.e("LogType==" + LogType);
        DebugLog.e("strchecknum==" + strchecknum);
        new MyOkHttpClient().doPost(AppConfig.LsysLog_CheckLogApprove, params,
                new EBCallback() {
                    @Override
                    public void onEBResponse(String response) {
                        DebugLog.e("工作日誌审批==" + response);
                        JSONObject myJsonObject;
                        Boolean isSuc;
                        try {
                            myJsonObject = new JSONObject(response);
                            isSuc = myJsonObject.getBoolean("isSuc");
                            if (isSuc) {
                                mWorkLogApproveCallback.onorkLogApproveSuccessed("");
                            } else {
                                mWorkLogApproveCallback
                                        .onWorkLogApproveFailed();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        dismissProgressDia();
                    }

                    @Override
                    public void onEBError(String errorMsg) {
                        mWorkLogApproveCallback.onWorkLogApproveFailed();
                        dismissProgressDia();
                    }
                }, "Authorization", spUtil.getAccessToken());
    }

    public interface WorkLogInfoCallback {
        public void onorkLogInfoSuccessed(WorkLogDetail str);

        public void onWorkLogInfoFailed();

    }

    public interface WorkLogApproveCallback {
        public void onorkLogApproveSuccessed(String str);

        public void onWorkLogApproveFailed();

    }

}
